package com.jservice.web.smkPayment;

import com.jservice.framework.idWorker.Sequence;
import com.jservice.framework.spring.SpringContextUtil;
import com.jservice.model.SmkOrder;
import com.jservice.model.SmkPayment;
import com.jservice.web.base.JBaseService;
import com.jservice.web.smkSn.SnService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * SmkPaymentService
 *
 * @author cwvian
 * @version 1.0
 * @date 2017/6/2
 */
@Service("smkPaymentService")
@Transactional(readOnly = true)
public class SmkPaymentService extends JBaseService {
	public static final SmkPayment DAO = new SmkPayment();

	public static SmkPaymentService me() {
		return SpringContextUtil.getBean("smkPaymentService");
	}

	@Resource
	private SnService snService;

	@Resource
	private Sequence idWorKer;

	public SmkPayment findBySn(String sn){
		return DAO.doFindFirst(" sn = ? ",sn);
	}


	@Transactional
	public SmkPayment create(SmkOrder order, SmkPayment.PaymentMethod paymentMethod, SmkPayment.PaymentType paymentType) {
		SmkPayment payment = new SmkPayment();
		payment.setId(idWorKer.nextIdStr());
		payment.setCreateDate(new Date());
		payment.setModifyDate(payment.getCreateDate());
		payment.setAmount(order.getTotalAmount());
		payment.setOrderId(order.getId());
		payment.setSn(snService.generate(SnService.Type.payment));
		payment.setPaymentMethod(paymentMethod.name());
		payment.setPayer(order.getIdCard());
		payment.setStatus(SmkPayment.PaymentStatus.WAIT.name());
		payment.setType(paymentType.name());

		if (payment.save()) {
			return payment;
		}else{
			throw new RuntimeException("付款单创建异常");
		}
	}
}
